package com.fakeDYChat.common.user.service.impl;

import com.fakeDYChat.common.user.domain.enums.RoleEnum;
import com.fakeDYChat.common.user.service.IRoleService;
import com.fakeDYChat.common.user.service.cache.UserCache;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class RoleServiceImpl implements IRoleService {

    private final UserCache userCache;

    public RoleServiceImpl(UserCache userCache) {
        this.userCache = userCache;
    }

    /**
     * 判断用户是否有对应权限
     *
     * @param uid      用户id
     * @param roleEnum 权限标识
     * @return boolean
     * @author Tang
     * @date 2024/1/12 19:32
     **/
    @Override
    public boolean hasPower(Long uid, RoleEnum roleEnum) {
        Set<Long> roleSet = userCache.getRoleSet(uid);
        return isAdmin(roleSet) || roleSet.contains(roleEnum.getType());
    }

    /**
     * 判断是否为超级管理员
     *
     * @param roleSet 权限集合
     * @return boolean
     * @author Tang
     * @date 2024/1/12 19:48
     **/
    private boolean isAdmin(Set<Long> roleSet) {
        return roleSet.contains(RoleEnum.ADMIN.getType());
    }
}
